﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace SmartyOcr
{
    /// <summary>
    /// 图片内容读取器 
    /// xue8000@gmail.com 
    /// modified @2012-11-11   
    /// </summary>
    public class ImageReader
    {
        /// <summary>
        /// 数字化图片像素点阵
        /// </summary>
        /// <param name="imagePath">图片的本地路径 </param>
        /// <returns></returns>
        public static List<List<int>> GetColorMatrix(string imagePath)
        {
            var img = new Bitmap(Bitmap.FromFile(imagePath));
            return GetColorMatrix(img);
        }
        /// <summary>
        /// 数字化图片像素点阵
        /// </summary>
        /// <param name="img">Bitmap对象</param>
        /// <returns></returns>
        public static List<List<int>> GetColorMatrix(Bitmap img)
        {
            var data = new List<List<int>>(img.Height);

            #region//抽取数字特征信息(把图片按顺时针方向旋转 90度,为的是切割方便)
            for (int i = 0; i < img.Width; i++)
            {
                var r = new List<int>(img.Height);
                for (int j = img.Height - 1; j > -1; j--)
                {
                    var rgb = img.GetPixel(i, j);
                    if (rgb.R == rgb.G && rgb.G == rgb.B)
                    {
                        r.Add(0);
                    }
                    else
                    {
                        r.Add(1);
                    }
                }
                data.Add(r);
            }
            #endregion
            return data;
        }
        /// <summary>
        /// 抽取图片数字化信息
        /// </summary>
        /// <param name="map">Bitmap对象</param>
        /// <returns></returns>
        public static List<string> ExtractContent(Bitmap map)
        {
            var data = GetColorMatrix(map);
            var charactoristic = new List<string>();
            #region//step 1.从图片中抽取特征信息
            var index = 0;
            var feauture = new StringBuilder(128);
            for (; index < data.Count; index++)
            {
                var groups = data[index].GroupBy(x => x);
                if (groups.Count() > 1)
                {
                    data[index].ForEach(x => feauture.Append(x));
                }
                else
                {
                    #region//取得特征信息
                    if (feauture.Length > 0)
                    {
                        var line = feauture.ToString();
                        charactoristic.Add(line);
                        feauture.Clear();
                    }
                    #endregion
                }
            }
            #endregion
            return charactoristic;
        }
        /// <summary>
        /// 抽取图片数字化信息
        /// </summary>
        /// <param name="imagePath">图片本地路径 </param>
        /// <returns></returns>
        public static List<string> ExtractContent(string imagePath)
        {
            var img = new Bitmap(Bitmap.FromFile(imagePath));
            return ExtractContent(img);
        }
        /// <summary>
        /// 导出网络上的图片
        /// </summary>
        /// <param name="uri">Uri</param>
        /// <returns>图片上字符的特征信息</returns>
        public static List<string> ExtractContent(Uri uri) {
            var client = new System.Net.WebClient();
            var stream = client.OpenRead(uri);
            var img = new Bitmap(stream);
            return ExtractContent(img);
        }
        /// <summary>
        /// 导出图片内容
        /// </summary>
        /// <param name="map">图片</param>
        /// <param name="token">图片内容</param>
        /// <returns>数字化的图片内容</returns>
        public static List<KeyValuePair<string, string>> Numerical(Bitmap map, char[] token)
        {
            var data = GetColorMatrix(map);
            var charactoristic = new List<KeyValuePair<string, string>>();
            #region//step 1.从图片中抽取特征信息
            var index = 0;
            var tokenIndex = 0;
            var feauture = new StringBuilder(128);
            for (; index < data.Count; index++)
            {
                var groups = data[index].GroupBy(x => x);
                if (groups.Count() > 1)
                {
                    data[index].ForEach(x => feauture.Append(x));
                }
                else
                {
                    #region//取得特征信息
                    if (feauture.Length > 0)
                    {
                        var line = feauture.ToString();
                        charactoristic.Add(new KeyValuePair<string,string>(token[tokenIndex].ToString(),line));
                        tokenIndex++;
                        feauture.Clear();
                    }
                    #endregion
                }
            }
            #endregion
            return charactoristic;
        }
    }
}